package gamebackend;
import generators.BasicBlockGenerator;
import generators.BlockRule;
import generators.HorizontalStreakGenerator;
import generators.VerticalScaredGenerator;
import generators.VerticalStreakGenerator;
import gui.BlockStyle;

import framework.Color;
import java.util.Random;



public class CrystalBiome extends GameSession.Biome {

	private static final double CHANGE_LENGTH = 100;
	private static final Block BLUE = new Block(BlockStyle.THORN, new Color(70, 100, 250));
	private static final Block PURPLE = new Block(BlockStyle.THORN, new Color(150, 100, 250));
	
	private static final Color BACK_START = new Color(0, 0, 0, 0);//60, 75, 235, 50);
	private static final Color BACK_FINISH = new Color(22, 22, 88, 250);
	
	private final Block my_back = new Block(BlockStyle.PLAIN, BACK_FINISH);
	private final double my_maxOverlap;
	
	private double my_distanceBeforeChange;
	public CrystalBiome(GameSession the_session,
			int the_tier, Random the_random, double the_length) {
		the_session.super(the_session, the_tier, the_random, the_length);
		
		addHardPath(new FlightPath(Block.SECONDARY_PATH, getHardPath(), 16,
				new StutterFlier(15, getWidth() - 15, getWidth() / 15.0, getRandom())), getLength());
		addEasyPath(new FlightPath(Block.PRIMARY_PATH, getEasyPath(), 20,
				new EasyFlier(getRandom(), 15, getGameSession().getWidth() - 15)), getLength());
		
		addGenerator(new VerticalScaredGenerator(BLUE, getRandom(), .005), getLength());
		addGenerator(new VerticalScaredGenerator(PURPLE, getRandom(), .005), getLength());
		addToMidGround(new BasicBlockGenerator(my_back, BlockRule.ANY, getRandom(), 1), getLength());
		my_maxOverlap = CHANGE_LENGTH *( (getTier() - 1) * 3.0 / 4  + .5);
	}
	public void step(final double the_yChange) {
		super.step(the_yChange);
		my_distanceBeforeChange -= the_yChange;
		final double factor = 1 - Math.abs(1 - 2 * getRemaining() / getLength());
		my_back.setColor(new Color((int) (BACK_FINISH.getRed() * factor + BACK_START.getRed() * (1 - factor)),
				(int) (BACK_FINISH.getGreen() * factor + BACK_START.getGreen() * (1 - factor)),
				(int) (BACK_FINISH.getBlue() * factor + BACK_START.getBlue() * (1 - factor)),
				(int) (BACK_FINISH.getAlpha() * factor + BACK_START.getAlpha() * (1 - factor))));
		if (my_distanceBeforeChange < 0) {
			my_distanceBeforeChange += CHANGE_LENGTH;
			final double length = Math.min(getRemaining(), (CHANGE_LENGTH + my_maxOverlap) * (.5 + .5 * factor));
			switch (getRandom().nextInt(4)) {
			case 0:
				addGenerator(new HorizontalStreakGenerator(getRandom(), .8, HorizontalStreakGenerator.RIGHT), length);
				break;
			case 1:
				addGenerator(new HorizontalStreakGenerator(getRandom(), .8, HorizontalStreakGenerator.LEFT), length);
				break;
			case 2:
			case 3:
				addGenerator(new VerticalStreakGenerator(getRandom(), .8), length);
				break;
			}
		}
	}

}
